<?php
    include_once("../action/checkAuthenticationAction.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<title>Quản lý sản phẩm</title>
	<?php 
        include("import-css.php");
        include("../util/constant.php");
        include("../util/message.php");
    ?>
    
</head>
<body>
	<?php include("header.php");?>
<form method="post">
	<div class="container-fluid">
		<div class="row-fluid">
			<?php 
                include("menu.php");
                include_once("../service/productService.php");
            ?>
			<div id="content" class="span10">
                <div id="msg" class="center alert"></div>
                <div class="row-fluid sortable">
    				<div class="box span12">
    					<div class="box-header well" data-original-title>
                        	<?php
								$require_perm = array("edit product");
								if(authUtil($require_perm,$perm_array,false)){
							?>
                                <button type="button" class="btn btn-primary" onclick="updatePrice();">
                                    <i class="icon-edit icon-white"></i>Cập nhật
                                </button>
                            <?php
								}
                            ?>
    					</div>
    					<div class="box-content">
                            
    						<table class="table table-striped table-bordered bootstrap-datatable datatable">
                                <thead>
                                    <tr>
                                        <th>STT</th>
                                        <th>Mã</th>
                                        <th>Tên</th>
                                        <th>Giá</th>
                                        <th>Status</th>
                                        <th>Khuyến mãi</th>
                                        <th>Chức năng</th>                                        
                                    </tr>
    							 </thead>   
    							 <tbody>
                                    <?php
                                        $count = 1;
                                        $products = getAllProducts(); 
                                        
                                        while($row = ConnectionManager::getInstance()->fetch($products)) {
                                    ?>
                                    <tr id="<?php echo $row['PRO_ID']; ?>">
                                        <td><?php echo $count++;?></td>
   									    <td><?php echo $row['PRO_CODE']; ?></td>
    									<td class="center"><?php echo $row["PRO_NAME"]; ?></td>
    									<td class="center">
                                            <input type="text" id="pri_<?php echo $row["PRO_ID"];?>" name="price" onkeyup="this.value = formatCurrency(this.value,2);" value="<?php echo number_format($row["PRO_PRICE"]); ?>"/>
                                        </td>
    									<td class="center">
											<select name="status" data-rel="chosen" style="width: 100px;" <?php if(authUtil("edit prodcut",$perm_array,true)){?>onchange="updateStatus(this.value, <?php echo $row["PRO_ID"];?>);"<?php };?>>
                                                <?php
                                                    $statuses = findAllProductStatuses();
                                                    while($status = mysql_fetch_array($statuses)){
                                                    if($status["PRO_STAT_ID"] == $row["PRO_STAT_ID"]){
                                                ?>
                                                            <option value="<?php echo $status["PRO_STAT_ID"];?>" selected><?php echo $status["PRO_STAT_NAME"];?></option>
                                                <?php
                                                        } else {
                                                ?>
                                                            <option value="<?php echo $status["PRO_STAT_ID"];?>"><?php echo $status["PRO_STAT_NAME"];?></option>
                                                <?php            
                                                        }
                                                    }
                                                ?>
											</select>	
    									</td>
                                        <td class="center">
											<select id="pro_<?php echo $row["PRO_ID"];?>" name="promotion" data-rel="chosen" style="width: 100px;" <?php if(authUtil("edit prodcut",$perm_array,true)){?>onchange="updatePromotion(this.id, this.value);"<?php }?>>
                                                <option value="">Không</option>
                                                <?php
                                                    $discounts = getAllDiscounts();
                                                    while($discountRow = ConnectionManager::getInstance()->fetch($discounts)){
                                                        if($row["DIS_ID"] == $discountRow["DIS_ID"]){
                                                ?>
												            <option value="<?php echo $discountRow["DIS_ID"];?>" selected><?php echo $discountRow["DIS_NAME"];?></option>
                                                        <?php
                                                        } else {
                                                        ?>
                                                            <option value="<?php echo $discountRow["DIS_ID"];?>"><?php echo $discountRow["DIS_NAME"];?></option>
                                                        <?php
                                                        }                                                            
                                                        ?>
                                                <?php
                                                    }
                                                ?>
											</select>	
    									</td>
                                        <td>
                                            <div class="modal hide fade" id="productModal<?php echo $row["PRO_ID"];?>"></div>
                                            <?php
												$require_perm = array("view product");
												if(authUtil($require_perm,$perm_array,true)){
											?>
                                                <a href="#productModal<?php echo $row["PRO_ID"];?>" role="button" class="btn btn-success" data-toggle="modal" onclick="getProductInformation(<?php echo $row["PRO_ID"];?>);">
                                                    <i class="icon-zoom-in icon-white"></i>  
                                                    Xem thông tin
                                                </a>
                                            <?php
												}
                                            ?>
                                            
                                            <?php
												$require_perm = array("edit product");
												if(authUtil($require_perm,$perm_array,true)){
											?>
                                                <a class="btn btn-info" href="product-detail.php?action=<?php echo EDIT_ACTION; ?>&id=<?php echo $row["PRO_ID"];?>">
                                                    <i class="icon-edit icon-white"></i>  
                                                    Cập nhật                                            
                                                </a>
                                            <?php
												}
                                            ?>
                                            <?php
												$require_perm = array("delete product");
												if(authUtil($require_perm,$perm_array,true)){
											?>
												<?php
                                                    if(!isDeletable($row["PRO_ID"])){
                                                ?>
                                                    <a class="btn btn-danger" href="#" onclick="if(confirmDelete()) deleteProduct(<?php echo $row["PRO_ID"]?>);">
                                                        <i class="icon-trash icon-white"></i> 
                                                        Xóa
                                                    </a>
                                                <?php
                                                    }
                                                ?>
                                            <?php
												}
                                            ?>
                                        </td>                                      
    								</tr> 
                                    <?php
                                        }
                                    ?>                         
    							 </tbody>
    						 </table>     
    					</div>
    				</div><!--/span-->
    			</div><!--/row-->
            </div><!-- end of content -->
        </div><!-- end of row-fluid -->
        <?php include("footer.php");?>
    </div><!-- container-fluid -->
    <?php include("import-js.php");?>
    <script>
		function deleteProduct(proId){
			$.ajax({  
                type: "POST",  
                url: "../action/ajax/deleteProductProcess.php",  
                data: "proId="+ proId,
                async: false,  
                success: function(data) {  
                    if(data == <?php echo DELETE_SUCCESSFULLY;?>) $("#" + proId).remove();
                }
            });
		}
		
        function updatePromotion(proId, promotionId){
            var dataString = "proId="+ proId + "&promotionId=" + promotionId;
            
            $.ajax({  
                type: "POST",  
                url: "../action/ajax/updatePromotionProcess.php",  
                data: dataString,
                async: false,  
                success: function(data) {  
                    if(data == <?php echo UPDATE_SUCCESSFULLY;?>){
                        showMessage("<?php echo $messages["editProductSuccessfully"];?>", "success");
                        
                    } else{
                        showMessage("<?php echo $messages["editProductFailed"];?>", "error");
                    }
                }
            });
        }
        
        function updatePrice(){
            validateCurrency('price');
            var ids = new Array();
            var prices = new Array();
            
            $('input[name^=price]').each(function() {
                ids.push($(this).attr('id'));
                prices.push($(this).val());
            });
            
            var dataString = "ids="+ ids + "&prices=" + prices;
            
            $.ajax({  
                type: "POST",  
                url: "../action/ajax/updatePriceProcess.php",  
                data: dataString,
                async: false,
                success: function(data) {  
                    if(data == <?php echo UPDATE_SUCCESSFULLY;?>){
                        showMessage("<?php echo $messages["editProductSuccessfully"];?>", "success");
                    } else{
                        showMessage("<?php echo $messages["editProductFailed"];?>", "error");
                    }
                    
                    $('input[name^=price]').each(function() {
                        var price = $(this).val();
                        $(this).val(formatCurrency(price, 2));
                    });
                }
            });
        }
        
        function updateStatus(status, proId){
            var dataString = "status=" + status + "&proId=" + proId;
            
            $.ajax({
                type: "POST",
                url: "../action/ajax/updateProductStatusProcess.php",
                data: dataString,
                success: function(data){
                    if(data == <?php echo UPDATE_SUCCESSFULLY;?>){
                        showMessage("<?php echo $messages["editProductStatusSuccessfully"];?>", "success");
                    } else{
                        showMessage("<?php echo $messages["editProductStatusFailed"];?>", "error");
                    }
                }
            });
        }
        
        function  getProductInformation(proId){
            var dataString = "proId=" + proId;
            
            $.ajax({
                type: "POST",
                url: "../action/ajax/getProductInformationProcess.php",
                data: dataString,
                success: function(data){
                    $("#productModal" + proId).html(data);
                }
            });
        }
    </script>
</form>
</body>
</html>